pandas提取日期的年份,月份

import pandas as pd
list_of_dates = ['2019-11-20', '2020-01-02', '2020-02-05','2020-03-10','2020-04-16']
employees=['Hisila', 'Shristi','Zeppy','Alina','Jerry']
df = pd.DataFrame({
'Joined date': pd.to_datetime(list_of_dates)}, index=employees)
df['Year'] = df['Joined date'].dt.year
df['Month'] = df['Joined date'].dt.month
print(df)

关键函数:df['Month'] = df['Joined date'].dt.month

筛选满足多条件的数据:

import pandas as pd
import numpy as np
import os
import matplotlib.pyplot as plt

path = './0area_jrc_raw'
filenames = os.listdir(path)

for filename in filenames:
    file_path = r'./0area_jrc_raw/' + filename
    df = pd.read_excel(file_path)
    
    df['Month'] = df['Date'].dt.month
    
    data = df[(df['Month'] >5) & (df['Month']<12)] #select 6-11 month data
    
    print(data['Month'])
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值